package com.sxhuayuan.parking.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.Length;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "hypk_admin")
public class Admin extends BaseEntity {

	private static final long serialVersionUID = 147618848115151889L;

	/**
	 * 用户名
	 */
	private String username = "";

	/**
	 * 密码
	 */
	private String password = "";

	/**
	 * 姓名
	 */
	private String name;

	/**
	 * 部门
	 */
	private String department;

	/**
	 * 是否启用
	 */
	private Boolean isEnabled = true;

	/**
	 * 是否锁定
	 */
	private Boolean isLocked = false;

	/**
	 * 连续登录失败次数
	 */
	private Integer loginFailureCount = 0;

	/**
	 * 锁定日期
	 */
	private Date lockedDate;

	/**
	 * 最后登录日期
	 */
	private Date loginDate;

	/**
	 * 最后登录IP
	 */
	private String loginIp;

	/**
	 * 微信openid
	 */
	private String openid;

	/**
	 * img图标
	 */
	private String imgUrl;

	/**
	 * 角色
	 */
	private Set<Role> roles = new HashSet<Role>();

	/**
	 * 获取用户名
	 *
	 * @return 用户名
	 */
	@NotEmpty(groups = Save.class)
	@Pattern(regexp = "^[0-9a-z_A-Z\\u4e00-\\u9fa5]+$")
	@Length(min = 2, max = 20)
	@Column(nullable = false, updatable = true, unique = true, length = 100)
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * 获取密码
	 *
	 * @return 密码
	 */
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	@NotEmpty(groups = Save.class)
	@Pattern(regexp = "^[^\\s&\"<>]+$", groups = Save.class)
	@Length(min = 4, max = 64, groups = Save.class)
	@Column(nullable = false)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * 获取姓名
	 *
	 * @return 姓名
	 */
	@Length(max = 100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 获取部门
	 *
	 * @return 部门
	 */
	@Length(max = 100)
	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	/**
	 * 获取是否启用
	 *
	 * @return 是否启用
	 */
	@NotNull
	@Column(nullable = false)
	public Boolean getIsEnabled() {
		return isEnabled;
	}

	public void setIsEnabled(Boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	/**
	 * 获取是否锁定
	 *
	 * @return 是否锁定
	 */
	@Column(nullable = false)
	public Boolean getIsLocked() {
		return isLocked;
	}

	public void setIsLocked(Boolean isLocked) {
		this.isLocked = isLocked;
	}

	/**
	 * 获取连续登录失败次数
	 *
	 * @return 连续登录失败次数
	 */
	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	@Column(nullable = false)
	public Integer getLoginFailureCount() {
		return loginFailureCount;
	}

	public void setLoginFailureCount(Integer loginFailureCount) {
		this.loginFailureCount = loginFailureCount;
	}

	@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
	public Date getLockedDate() {
		return lockedDate;
	}

	public void setLockedDate(Date lockedDate) {
		this.lockedDate = lockedDate;
	}

	public Date getLoginDate() {
		return loginDate;
	}

	public void setLoginDate(Date loginDate) {
		this.loginDate = loginDate;
	}

	public String getLoginIp() {
		return loginIp;
	}

	public void setLoginIp(String loginIp) {
		this.loginIp = loginIp;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	/**
	 * 获取角色
	 *
	 * @return 角色
	 */
	@NotEmpty
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "hypk_admin_role")
	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public String getImgUrl() {
		return imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

}
